/**
 * Created with JetBrains WebStorm.
 * User: Doan Hong Thai
 * Date: 4/29/13
 * Time: 8:20 PM
 * To change this template use File | Settings | File Templates.
 */
var MyText = Class.create(MyElement, {
    initialize: function($super, o){
        $super(o);
        this.className = "MyText";
        $j(this.divTag).addClass("my-text");
    },
    initAttributeList: function($super, o){
        $super(o);
        var _this = this;

        $j.each(o.element, function(){
            if(this.type == "text"){
                _this.attributeList.add(new MyAttribute(this));
                var textTag = new Element("p");
                textTag.innerHTML = this.value;
                textTag.setStyle({"width": "100%", "height": "100%"});
                _this.divTag.insert(textTag);
            }
        });
    },
    reloadAttributeList: function($super, o){
        $super(o);
        var _this = this;

        $j.each(o.element, function(){
            if(this.type == "text"){
                _this.attributeList.add(new MyAttribute(this));
                var textTag = $j(_this.divTag).find("p:first");
                textTag.innerHTML = this.value;
            }
        });
    },
    setValueByKey: function($super, key, value){
        if($super(key, value)){
            return true;
        }
        else{
            var att = this.attributeList.getAttributeFromKey(key);
            if(att !== null){
                var matched = false;

                if(att.type == "text"){
                    var textTag = $(this.id).firstDescendant();
                    if(textTag !== null){
                        $(textTag).innerHTML = value;
                        matched = true;
                    }
                }
                if(matched){
                    this.attributeList.setValueByKey(key, value);
                    return true;
                }
            }
        }
        return false;
    },
    setValueByName: function($super, name, value){
        if($super(name, value)){
            return true;
        }
        else{
            var att = this.attributeList.getAttributeFromName(name);
            if(att !== null){
                var matched = false;

                if(att.type == "text"){
                    var textTag = $(this.id).firstDescendant();
                    if(textTag !== null){
                        $(textTag).innerHTML = value;
                        matched = true;
                    }
                }
                if(matched){
                    this.attributeList.setValueByName(name, value);
                    return true;
                }
            }
        }
        return false;
    }
})